﻿using System;
using System.Collections.Generic;
using NFinal;
using System.Threading.Tasks;

namespace System
{
    public static class ActionExtension
    {
        public static void SetContentType(this IAction action, string contentType)
        {
            action.SetResponseHeader(Constant.HeaderContentType, contentType);
        }
        public static void Write(this IAction action, NFinal.Validation.ValidResult result)
        {
            foreach (var r in result)
            {
                action.Write(r.message);
            }
        }
        public static void Write(this IAction action, ObjectContainer obj)
        {
            action.Write(obj.value.ToString());
        }
        public static void Write(this IAction action, StringContainer obj)
        {
            action.Write(obj.value);
        }
        /// <summary>
        /// 返回json{code:1,msg:"",result:[json字符串]}
        /// </summary>
        /// <param name="json">json字符串</param>
        public static void AjaxReturn(this IAction action, string json)
        {
            action.SetResponseHeader(NFinal.Constant.HeaderContentType, "text/json ;charset=utf-8");
            action.Write(json);
        }
        /// <summary>
        /// 页面重定向
        /// </summary>
        /// <param name="url"></param>
        public static void Redirect(this IAction action, string url)
        {
            action.SetResponseStatusCode(302);
            action.SetResponseHeader(NFinal.Constant.HeaderContentType, "text/html ;charset=utf-8");
            action.SetResponseHeader("Location", url);
        }
        /// <summary>
        /// 返回数据库实体类对应的json对象{code:1,msg:"",result:[json对象]}
        /// </summary>
        /// <param name="str">数据库实体类</param>
        public static void AjaxReturn(this IAction action, NFinal.IModel model)
        {
            if (model == null)
            {
                action.AjaxReturn("null");
            }
            else
            {
                action.AjaxReturn(model?.ToJson());
            }
        }
        /// <summary>
        /// 返回数据库实体类对应的json对象{code:1,msg:"",result:[json对象]}
        /// </summary>
        /// <param name="obj">数据库实体类</param>
        public static void AjaxReturn(this IAction action, dynamic obj)
        {
            action.AjaxReturn((string)obj.ToJson());
        }
        /// <summary>
        /// 返回数据库实体类对应的json对象{code:[code状态码],msg:[msg消息],result:[json对象]}
        /// </summary>
        /// <param name="str">数据库实体类</param>
        /// <param name="code">状态码</param>
        /// <param name="msg">消息</param>
        public static void AjaxReturn(this IAction action, NFinal.IModel model, int code, string msg)
        {
            if (model == null)
            {
                action.AjaxReturn("null", code, msg);
            }
            else
            {
                action.AjaxReturn(model.ToJson(), code, msg);
            }
        }
        /// <summary>
        /// 返回true或false
        /// </summary>
        /// <param name="obj">bool变量</param>
        public static void AjaxReturn(this IAction action, bool obj)
        {
            action.SetResponseHeader(NFinal.Constant.HeaderContentType, "application/json;charset=utf-8");
            action.Write(obj ? "true" : "false");
        }
        /// <summary>
        /// 返回表数据对象对应的json字符串{code:[code状态码],msg:[msg消息],result:[表数据对应的json对象]}
        /// </summary>
        /// <typeparam name="T">表数据，NFinal.List对象</typeparam>
        /// <param name="obj">表数据对应的json对象</param>
        public static void AjaxReturn<T>(this IAction action, T obj) where T : NFinal.IModel, IList<NFinal.IModel>
        {
            action.AjaxReturn(obj.ToJson(), obj == null ? 1 : 0, "");
        }
        /// <summary>
        /// 返回json字符串{code:[code状态码],msg:[msg消息],result:[json字符串]}
        /// </summary>
        /// <param name="json">json字符串</param>
        /// <param name="code">状态码</param>
        /// <param name="msg">消息</param>
        public static void AjaxReturn(this IAction action, string json, int code, string msg)
        {
            action.SetResponseHeader(Constant.HeaderContentType, "application/json;charset=utf-8");
            action.Write(string.Format("{{\"code\":{0},\"msg\":\"{1}\",\"result\":{2}}}", code, msg, json));
        }
        /// <summary>
        /// 输出一行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public static void WriteLine<T>(this IAction action, T obj)
        {
            action.Write(obj.ToString());
            action.Write("<br/>");
        }
        /// <summary>
        /// 输出字符串
        /// </summary>
        /// <param name="obj"></param>
        public static void Write(this IAction action, string obj)
        {
            if (obj != null)
            {
                action.Write(Constant.encoding.GetBytes(obj));
            }
        }
        /// <summary>
        /// 输出字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public static void Write<T>(this IAction action,T obj)
        {
            action.Write(obj.ToString());
        }
        /// <summary>
        /// 输出字节流
        /// </summary>
        /// <param name="buffer"></param>
        public static void Write(this IAction action,byte[] buffer)
        {
            if (buffer != null)
            {
                action.Write(buffer, 0, buffer.Length);
            }
        }
        /// <summary>
        /// 是否是手机端
        /// </summary>
        /// <param name="userAgent"></param>
        /// <returns></returns>
        public static bool IsMobile(this IAction action, string userAgent)
        {
            string agent = userAgent;
            bool isMobile = false;
            string[] keywords = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" };

            //排除 Windows 桌面系统  
            if (!agent.Contains("Windows NT") || (agent.Contains("Windows NT") && agent.Contains("compatible; MSIE 9.0;")))
            {
                //排除 苹果桌面系统  
                if (!agent.Contains("Windows NT") && !agent.Contains("Macintosh"))
                {
                    foreach (string item in keywords)
                    {
                        if (agent.Contains(item))
                        {
                            isMobile = true;
                            break;
                        }
                    }
                }
            }
            return isMobile;
        }
        /// <summary>
        /// 返回服务器路径
        /// </summary>
        /// <param name="path">相对路径</param>
        /// <returns></returns>
        public static string MapPath(this IAction action, string path)
        {
            return NFinal.Utility.MapPath(path);
        }
        /// <summary>
        /// 渲染html模板
        /// </summary>
        /// <param name="tplPath">模板相对路径,一般相对于App/Views/Default目录</param>
        public static void View(this IAction action, string tplPath) { }
        /// <summary>
        /// 渲染html模板
        /// </summary>
        /// <param name="name">模板样式</param>
        /// <param name="tplPath">模板相对路径,一般相对于App/Views/Default目录</param>
        public static void View(this IAction action,string name, string tplPath) { }

    }
}
